﻿using System;
using System.Data;
using System.Web.UI.WebControls;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class EnterpriseUserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteSystemMaster)Master).InitMenu();
            ((SiteSystemMaster)Master).li1.Attributes.Add("class", "hover ccc");
            ((SiteSystemMaster)Master).b_hmenu1.Style.Clear();
            ((SiteSystemMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu3.Style.Add("display", "none");
            ((SiteSystemMaster)Master).s_menu1_1.Style.Add("COLOR", "#147ab8");
            ((SiteSystemMaster)Master).s_menu1_1.Style.Add("TEXT-DECORATION", "none"); 

            /*判断是否是管理员身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../System/Login.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    ViewState["orderby"] = "users_totalconsumemoney";
                    ViewState["asc"] = "desc";
                    bind();
                }   
            }
        }

        private void bind()
        {
            EnterpriseUserBLL temp = new EnterpriseUserBLL();
            DataSet ds = temp.GetListAll();

            DataView dv = ds.Tables[0].DefaultView;
            dv.Sort = (String)ViewState["orderby"] + " " + (String)ViewState["asc"];

            grVEnterpriseList.DataSource = dv;
            grVEnterpriseList.DataBind();
        }

        protected void CB_SelectAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.grVEnterpriseList.Rows.Count; i++)
            {
                GridViewRow gr = this.grVEnterpriseList.Rows[i];
                CheckBox chk = (CheckBox)gr.Cells[0].FindControl("CB_Select");
                chk.Checked = this.CB_SelectAll.Checked; //跟随全选按扭的状态变化;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            EnterpriseUserBLL temp = new EnterpriseUserBLL();
            int selectCount = 0;
            int users_id = 0; 
            foreach (GridViewRow gr in grVEnterpriseList.Rows)
            {
                CheckBox chk = (CheckBox)gr.Cells[0].FindControl("CB_Select");
                if (chk.Checked) 
                {
                    users_id = int.Parse(grVEnterpriseList.DataKeys[gr.RowIndex].Values[0].ToString()); 
                    temp.Delete(users_id);
                    selectCount++;
                }
            }
           
            if (0 == selectCount)
            {
                //MessageBox.Show("对不起，你没有选择记录!");
            }

            temp = null;
            Response.Redirect(Request.Url.ToString());
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/System/AddEnterpriseUser.aspx");
        }

        protected void grVEnterpriseList_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            if (ViewState["orderby"].ToString() == sPage)
            {
                if (ViewState["asc"].ToString() == "desc")
                    ViewState["asc"] = "asc";
                else
                    ViewState["asc"] = "desc";
            }
            else
            {
                ViewState["orderby"] = e.SortExpression;
            }
            bind();
        }
    }
}